clear all
set more off
	
global dir = "C:\Users\mrueda\Documents\Emory\Papers\Networks_persistance\do_files\do_files_APSA21\post_JOP\Replication_BJPS\"	

cd "$dir"

use "Data\cand_level_persist_rep.dta",clear


gen treat=0
replace treat=1 if margin_victory>0&margin_victory~=.
replace treat=. if margin_victory==.

gen treat_margin_victory=treat*margin_victory



	texdoc close 
	cap erase "$dir/Tables/Table2.tex"
	texdoc init "$dir/Tables/Table2.tex", force

	tex \begin{table}[h]
	tex \caption{Effect of electoral victory on benefits to donors}\label{tab:table_benefits}
	tex \centering
	tex \begin{tabular}{l H H c c c c} \hline
	tex Outcome:& &&\multicolumn{2}{c}{Receive contract}&Receive contract& Runs in 2015\\
	tex &  & &\multicolumn{2}{c}{(municipality)} & (outside)& \\
	tex & Non-family &Family & Non-Family &Family  & Family &Family \\
	tex & (1) & (2) & (1) & (2)& (3) & (4)\\ \hline
	tex & & & & & &\\

	
	*Model 1
	foreach x in  contract{
	
		*No Family
		*Regressions
		quietly: regress nf`x' treat treat_margin_victory margin_victory , vce(cluster muni_code)
		quietly sum nf`x' if e(sample)
			local nfmean_`x' : di %5.3f r(mean)
			local nfsd_`x' : di %5.3f r(sd) 		
		
		rdrobust nf`x' margin_victory , vce(cluster muni_code)

		*Local's for the table
		local nfbw_`x' : di %5.2f `e(h_l)'
		local nfNeff_`x' = `e(N_h_l)'+`e(N_h_r)'
		local nfN_`x' = `e(N)'
		local nfbeta1_`x' : di %5.3f `e(tau_cl)'
		local nfbeta2_`x' : di %5.3f `e(tau_bc)'

		*Confidence intervals
			local nfser1_`x' : di %5.3f `e(ci_l_rb)'
			local nfser2_`x' : di %5.3f `e(ci_r_rb)'
			
/* HERE*/	local nfem1_`x' = (`nfbeta1_`x''/`nfmean_`x'')*100 
			local nfem1_`x' : di %5.2f `nfem1_`x''
			
		*P-values
		local nfpval2_`x' : di %5.3f `e(pv_rb)'
		scalar nfpval2_`x' = e(pv_rb)
		
		regress nf`x' treat treat_margin_victory margin_victory , vce(cluster muni_code)

		local nfN_`x' : di %5.0f e(N)
		local nfR2_`x' : di %5.3f e(r2)

		matrix b = e(b)
		matrix v = e(V)
		matrix res=r(table)
		
		local nfb1_`x' : di %5.3f b[1,1]
		local nfse1_`x' : di %5.3f sqrt(v[1,1])
		local nfp_v_`x' :di %5.3f res[4,1]
		local nfuci_`x': di %5.3f res[6,1]
		local nflci_`x': di %5.3f res[5,1]
		

	}
		
		*Family
		*Regressions


use "Data\cand_level_persist_rep.dta",clear

gen treat=0
replace treat=1 if margin_victory>0&margin_victory~=.
replace treat=. if margin_victory==.

gen treat_margin_victory=treat*margin_victory


	foreach x in   contract got_above_ext runs_any{
	
		*No Family
		*Regressions
		quietly: regress f`x' treat treat_margin_victory margin_victory , vce(cluster muni_code)
		quietly sum f`x' if e(sample)
			local fmean_`x' : di %5.3f r(mean)
			local fsd_`x' : di %5.3f r(sd) 		
		
		rdrobust f`x' margin_victory , vce(cluster muni_code)

		*Local's for the table
		local fbw_`x' : di %5.2f `e(h_l)'
		local fNeff_`x' = `e(N_h_l)'+`e(N_h_r)'
		local fN_`x' = `e(N)'
		local fbeta1_`x' : di %5.3f `e(tau_cl)'
		local fbeta2_`x' : di %5.3f `e(tau_bc)'

		*Confidence intervals
			local fser1_`x' : di %5.3f `e(ci_l_rb)'
			local fser2_`x' : di %5.3f `e(ci_r_rb)'
			
/* HERE*/	local fem1_`x' = (`fbeta1_`x''/`fmean_`x'')*100 
			local fem1_`x' : di %5.2f `fem1_`x''
			
		*P-values
		local fpval2_`x' : di %5.3f `e(pv_rb)'
		scalar fpval2_`x' = e(pv_rb)
		
		regress f`x' treat treat_margin_victory margin_victory , vce(cluster muni_code)

		local fN_`x' : di %5.0f e(N)
		local fR2_`x' : di %5.3f e(r2)

		matrix b = e(b)
		matrix v = e(V)
		matrix res=r(table)
		
		local fb1_`x' : di %5.3f b[1,1]
		local fse1_`x' : di %5.3f sqrt(v[1,1])
		local fp_v_`x' :di %5.3f res[4,1]
		local fuci_`x': di %5.3f res[6,1]
		local flci_`x': di %5.3f res[5,1]
		

	}
		
	

	*Continue table
	tex \multicolumn{3}{l}{Local linear}&&&&\\

	tex Electoral victory & `nfbeta1_total_cont_num_d' &  `fbeta1_total_cont_num_d'& `nfbeta1_contract' & `fbeta1_contract' & `fbeta1_got_above_ext' & `fbeta1_runs_any' \\
	
	tex \ \ \ \ Robust p-value & `nfpval2_total_cont_num_d' &  `fpval2_total_cont_num_d' & `nfpval2_contract' & `fpval2_contract' & `fpval2_got_above_ext' & `fpval2_runs_any' \\
	
	
	
	tex \ \ \ \ CI 95\%  & [`nfser1_total_cont_num_d',`nfser2_total_cont_num_d'] & [`fser1_total_cont_num_d',`fser2_total_cont_num_d'] &  [`nfser1_contract',`nfser2_contract'] & [`fser1_contract',`fser2_contract'] & [`fser1_got_above_ext',`fser2_got_above_ext'] & [`fser1_runs_any',`fser2_runs_any']\\
	tex & & & \\
	tex \multicolumn{3}{l}{Parametric (linear)}&&&&\\

	tex Electoral victory & `nfb1_total_cont_num_d' &  `fb1_total_cont_num_d'& `nfb1_contract' & `fb1_contract' & `fb1_got_above_ext' & `fb1_runs_any' \\
	
	tex \ \ \ \  p-value & `nfp_v_total_cont_num_d' &  `fp_v_total_cont_num_d' & `nfp_v_contract' & `fp_v_contract' & `fp_v_got_above_ext' & `fp_v_runs_any' \\
	
	
	
	tex \ \ \ \ CI 95\%  & [`nflci_total_cont_num_d',`nflci_total_cont_num_d'] & [`flci_total_cont_num_d',`fuci_total_cont_num_d'] &  [`nflci_contract',`nfuci_contract'] & [`flci_contract',`fuci_contract'] & [`flci_got_above_ext',`fuci_got_above_ext'] & [`flci_runs_any',`fuci_runs_any']\\
	tex & & & \\
	
	tex Observations &`nfN_total_cont_num_d' &  `fN_total_cont_num_d' & `nfN_contract'& `fN_contract' & `fN_got_above_ext' & `fN_runs_any' \\
	tex Bandwidth obs. & `nfNeff_total_cont_num_d' & `fNeff_total_cont_num_d' & `nfNeff_contract'& `fNeff_contract' & `fNeff_got_above_ext' & `fNeff_runs_any' \\
	tex Mean & `nfmean_total_cont_num_d' &`fmean_total_cont_num_d'  &  `nfmean_contract' & `fmean_contract' & `fmean_got_above_ext' & `fmean_runs_any' \\
	tex Bandwidth & `nfbw_total_cont_num_d' &  `fbw_total_cont_num_d' &`nfbw_contract' & `fbw_contract' & `fbw_got_above_ext' & `fbw_runs_any' \\ \hline
	tex \end{tabular}
	tex \parbox{160mm}{ \footnotesize{Local linear estimates of average treatment effects at the cutoff estimated with triangular kernel weights and optimal MSE bandwidth. Robust p-values with clustering at the municipality level and 95\% robust confidence intervals are computed following \cite{calonico_robust_2014}. The parametric linear model specification includes interaction of the treatment with running variable and running variable. Bandwidth obs. denotes the number of observations in the optimal MSE bandwidth.
	tex }
	tex }
	tex \end{table}
	cap texdoc close 

	